import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-04-15
 * Time: 20:31
 */
public class BinaryTree {

    static class TreeNode{
        public char val;
        public TreeNode left;//存储左子树的引用
        public TreeNode right;//存储又子树的引用

        public TreeNode(char val){
            this.val = val;
        }
    }

    public TreeNode creakTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //有返回值的前序遍历
    List<Character> list = new LinkedList<>();
    public List<Character> preOrder2(TreeNode root){
        if(root == null){
            return list;
        }
        list.add(root.val);
        preOrder2(root.left);
        preOrder2(root.right);
        return list;
    }

    //有返回值的前序遍历 左右子类法
    public List<Character> preOrder3(TreeNode root){
        List<Character> list = new LinkedList<>();
        if(root == null){
            return list;
        }
        list.add(root.val);
        List<Character> leftTree = preOrder3(root.left);
        list.addAll(leftTree);
        List<Character> rightTree = preOrder3(root.right);
        list.addAll(rightTree);

        return list;
    }

    //前序遍历(非递归写法)
    public void preOrder4(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    //中序遍历
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        preOrder(root.left);
        System.out.print(root.val + " ");
        preOrder(root.right);
    }

    //中序遍历非递归写法
    public void inOrder2(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    //后序遍历
    public void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }

    //后序遍历非递归写法
    public void postOrder2(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode pre = null;

        while(cur != null || stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }

            TreeNode top = stack.peek();
            if(top.right == null || top.right == pre){
                System.out.print(top.val + " ");
                stack.pop();
                pre = top;
            }else {
                cur = top.right;
            }
        }
    }

    //层序遍历
    public void levelOrder(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode cur = root;
        queue.offer(root);

        while(!queue.isEmpty()){
            cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }
    }

    //层序遍历的方法2
    public List<List<Character>> levelOrder2(TreeNode root){
        List<List<Character>> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while(size != 0){
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }

        return ret;
    }

    //节点的个数
    public static int nodeSize;
    public void size(TreeNode root){
        if(root == null){
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    public int size2(TreeNode root){
        if(root == null){
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }

    //叶子的数量
    public static int leafSize;
    public void leafSize(TreeNode root){
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null){
            leafSize++;
        }
        leafSize(root.left);
        leafSize(root.right);
    }

    //子类写法
    public int leafSize2(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return leafSize2(root.left) + leafSize2(root.right);
    }

    //获得树的第k层节点的个数
    public int getLevelNode(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getLevelNode(root.left,k - 1) + getLevelNode(root.right, k - 1);
    }

    //获得树的高度
    public int getHight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHight = getHight(root.left);
        int rightHight = getHight(root.right);

        return Math.max(leftHight,rightHight) + 1;
    }

    //在二叉树中查找val值
    public TreeNode getVal(TreeNode root, int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }

        TreeNode leftFind = getVal(root.left, val);
        if(leftFind != null){
            return leftFind;
        }
        TreeNode rightFind = getVal(root.right, val);
        if(rightFind != null){
            return rightFind;
        }
        return null;
    }

    //反转二叉树
    public TreeNode inverTree(TreeNode root){
        if(root == null){
            return null;
        }
        if(root.left == null && root.right == null){
            return root;
        }

        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        inverTree(root.left);
        inverTree(root.right);

        return root;
    }

}
